<template>
  <div class="register-container">
      <div class="register-box">
          <h2>用户注册</h2>
          <el-form :model="registerForm" :rules="rules" ref="registerForm">
              <el-form-item prop="username">
                  <el-input v-model="registerForm.username" placeholder="用户名">
                      <template #prefix>
                          <UserOutlined class="site-form-item-icon" />
                      </template>
                  </el-input>
              </el-form-item>
              <el-form-item prop="password">
                  <el-input v-model="registerForm.password" type="password" placeholder="密码">
                      <template #prefix>
                          <LockOutlined class="site-form-item-icon" />
                      </template>
                  </el-input>
              </el-form-item>
              <el-form-item prop="confirmPassword">
                  <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码">
                      <template #prefix>
                          <LockOutlined class="site-form-item-icon" />
                      </template>
                  </el-input>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" @click="handleRegister" :loading="loading" block>注册</el-button>
              </el-form-item>
              <div class="login-link">
                  <router-link to="/login">已有账号？立即登录</router-link>
              </div>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: 'RegisterPage',
  data() {
      const validatePass2 = (rule, value, callback) => {
          if (value !== this.registerForm.password) {
              callback(new Error('两次输入的密码不一致'))
          } else {
              callback()
          }
      }
      return {
          registerForm: {
              username: '',
              password: '',
              confirmPassword: ''
          },
          loading: false,
          rules: {
              username: [
                  { required: true, message: '请输入用户名', trigger: 'blur' },
                  { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
              ],
              password: [
                  { required: true, message: '请输入密码', trigger: 'blur' },
                  { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
              ],
              confirmPassword: [
                  { required: true, message: '请再次输入密码', trigger: 'blur' },
                  { validator: validatePass2, trigger: 'blur' }
              ]
          }
      }
  },
  methods: {
      handleRegister() {
          this.$refs.registerForm.validate(async valid => {
              if (valid) {
                  this.loading = true
                  try {
                      await this.$store.dispatch('user/register', {
                          username: this.registerForm.username,
                          password: this.registerForm.password
                      })
                      this.$message.success('注册成功')
                      this.$router.push('/login')
                  } catch (error) {
                      this.$message.error(error.message || '注册失败')
                  } finally {
                      this.loading = false
                  }
              }
          })
      }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.register-box {
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.register-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.login-link {
  text-align: center;
  margin-top: 15px;
}
</style>